#include <cxcore.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <math.h>
#include <direct.h>

int main(){
    IplImage * resim;
    CvScalar s;
  
    int **dizi,i,j;
    
        resim = cvLoadImage("ben.jpg",1);
    
   IplImage* ycb = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 3);
   IplImage* yeni = cvCreateImage(cvSize(resim->width,resim->height), IPL_DEPTH_8U, 3);
    if( !resim )
        return -1;

    int max_y=0,yt;
    int sum = 0, sum_r = 0, sum_g = 0, sum_b = 0, r, g, b;
    cvCvtColor(resim,ycb,CV_BGR2YCrCb);
    for(i=0;i<(ycb->height)-1;i++){
      for(j=0;j<(ycb->width)-1;j++){
         s=cvGet2D(ycb,i,j);
         if (s.val[0] > max_y) max_y =(int)s.val[0];
           }
        }
        
        yt = static_cast<int> (0.95 * (double)max_y);
        
         
        
   for(i=0;i<(resim->height)-1;i++){
      for(j=0;j<(resim->width)-1;j++){
         if ((int)((uchar *)(ycb->imageData + i*ycb->widthStep))[j*ycb->nChannels + 0] >= yt){
   			sum_r +=(int)((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 2];
			sum_g +=(int)((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 1];
			sum_b +=(int)((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 0];
			sum++;
            }
           }
        }
   for(i=0;i<(resim->height)-1;i++){
      for(j=0;j<(resim->width)-1;j++){
		if (sum >= 100) {
			r = (sum * 255 * (int)((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 2]) / sum_r;
			g = (sum * 255 * (int)((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 1]) / sum_g;
			b = (sum * 255 * (int)((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 0]) / sum_b;
			((uchar *)(yeni->imageData + i*yeni->widthStep))[j*yeni->nChannels + 2] = (r > 255) ? 255 : r;
			((uchar *)(yeni->imageData + i*yeni->widthStep))[j*yeni->nChannels + 1] = (g > 255) ? 255 : g;
			((uchar *)(yeni->imageData + i*yeni->widthStep))[j*yeni->nChannels + 0] = (b > 255) ? 255 : b;
		}else{
	((uchar *)(yeni->imageData + i*yeni->widthStep))[j*yeni->nChannels + 0]=((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 0];
    ((uchar *)(yeni->imageData + i*yeni->widthStep))[j*yeni->nChannels + 1]=((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 1]; 
    ((uchar *)(yeni->imageData + i*yeni->widthStep))[j*yeni->nChannels + 2]=((uchar *)(resim->imageData + i*resim->widthStep))[j*resim->nChannels + 2];
        }
           }
        }
       cvSaveImage("lc.jpg",yeni);
    cvNamedWindow( "result", 1 );
    cvNamedWindow( "result1", 1 );
    cvNamedWindow( "result2", 1 );
    cvShowImage( "result", resim );
    cvShowImage( "result1", yeni );
    cvShowImage( "result2", ycb );
    cvWaitKey( -1 );
    cvReleaseImage( &resim );
        cvReleaseImage( &ycb );
           cvReleaseImage( &yeni );
    cvDestroyWindow("result");
    cvDestroyWindow("result1");
    cvDestroyWindow("result2");
 /*    for(i=0;i<resim->width;i++){
        free(dizi[i]);
        }
     free(dizi);*/
    system("pause");
    return 0;
    }
